package com.app.studentActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.R;
import com.app.bean.QingjiaBean;
import com.app.bean.UserBean;
import com.app.util.AdapterUtil;
import com.app.util.DBHelper4;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPStaticUtils;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

public class Qingjia extends AppCompatActivity {

    private RecyclerView recy;
    private List<QingjiaBean> list;
    private UserBean userBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qingjia);
        String user = SPStaticUtils.getString("user");
        userBean = GsonUtils.fromJson(user, UserBean.class);
        initView();
        initData();
    }

    private void setAdapter() {
        AdapterUtil<QingjiaBean> adapter = new AdapterUtil<>(R.layout.qingjia_item, ((data, position, holder) -> {
            TextView title = holder.getView(R.id.title);
            TextView content = holder.getView(R.id.content);
            TextView username = holder.getView(R.id.username);
            TextView date = holder.getView(R.id.date);
            TextView status = holder.getView(R.id.status);
            ImageView image = holder.getView(R.id.image);
            LinearLayout layout = holder.getView(R.id.layout);

            Glide.with(Qingjia.this).load(list.get(position).getPic()).into(image);
            title.setText(data.getTitle());
            content.setText(data.getContent());
            username.setText("请假人："+data.getUsername());
            date.setText("请假时间："+data.getTime());
            if(data.getStatus().equals("0")){
                status.setText("状态：未批准");
            }else {
                status.setText("状态：已批准");
            }

            layout.setOnClickListener(v -> {
                SPStaticUtils.put( data.getId()+"", GsonUtils.toJson(data));
                Intent intent = new Intent(this, QingjiaDetail.class);
                intent.putExtra("bean", data.getId());
                startActivity(new Intent(intent));
            });
        }));
        adapter.listUpdate(list);
        recy.setAdapter(adapter);
    }

    private void initData() {
        list = new ArrayList<>();
        DBHelper4 dbHelper1 = new DBHelper4(this);
        SQLiteDatabase db = dbHelper1.getReadableDatabase();
        //query 查询
        String selection = "userid=?";
        String[] selectionArgs = {String.valueOf(userBean.getUser_id())};
        Cursor cursor;
        if(userBean.getUser_id().substring(0, 1).equals("j") || userBean.getUser_id().equals("admin")){
            cursor = db.query("qingjia", null, null, null, null, null, "date desc");
        }else {
            cursor = db.query("qingjia", null, selection, selectionArgs, null, null, "date desc");
        }

        //从数据库查询，获取游标，遍历，把数据记录转换成java bean
        while (cursor.moveToNext()) {
            QingjiaBean bean = new QingjiaBean();
            bean.setId(cursor.getInt(cursor.getColumnIndex("id")));
            bean.setTitle(cursor.getString(cursor.getColumnIndex("title")));
            bean.setContent(cursor.getString(cursor.getColumnIndex("content")));
            bean.setTime(cursor.getString(cursor.getColumnIndex("date")));
            bean.setPic(cursor.getBlob(cursor.getColumnIndex("pic")));
            bean.setUsername(cursor.getString(cursor.getColumnIndex("username")));
            bean.setStatus(cursor.getString(cursor.getColumnIndex("status")));
            bean.setUserid(cursor.getString(cursor.getColumnIndex("userid")));
            list.add(bean);
        }
        cursor.close();
        db.close();
        setAdapter();
    }

    private void initView() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.getChildAt(0).setOnClickListener(v -> finish());
        ((TextView) toolbar.getChildAt(1)).setText("请假");
        ((TextView) toolbar.getChildAt(2)).setText("添加");
        if(userBean.getUser_id().substring(0, 1).equals("j") || userBean.getUser_id().equals("admin")){
            ((TextView) toolbar.getChildAt(2)).setVisibility(View.GONE);
        }else {
            ((TextView) toolbar.getChildAt(2)).setVisibility(View.VISIBLE);
        }

        ((TextView) toolbar.getChildAt(2)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转界面，并把数据传递过去，传递数据的名字是bean
                Intent intent = new Intent(Qingjia.this, QingjiaAdd.class);
                startActivity(intent);
            }
        });
        recy = findViewById(R.id.recy);
        recy.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }
}